import { createStore } from 'vuex'
import { post } from '@/netWorke/manage';

export default createStore({
  state: {
    token: window.localStorage.getItem('token')
  },
  getters: {
    getToken(state){
      return state.token
    }
  },
  mutations: {
    setToken(state, token) {
      state.token = token
    }
  },
  actions: {
    login({ commit }, userinfo) {
      return new Promise((resolve,reject)=>{
        // 发送请求接口
      let url = '/api/login'
      post(url, userinfo).then(res => {
        console.log('res', res)
        if (res.status === 200) {
           /**
           * 1、设置vuex
           * 2、并在vuex中将token保存到本地
           */
          commit('setToken',res.token)
          window.localStorage.setItem('token', res.token);
          resolve(res)
        } else {
          console.log(res.message);
          reject(res)
        }
      })
      })
      
    }
  },
  modules: {
  }
})
